let Url = {
  parseUrl: function (url) {
    var match = url.match(/^((http|https):\/\/)?([^:/]*)(:(\d*))?([^?#]*)?(\?[^#]*)?(#.*)?$/);
    return {
      protocol: match[2],
      hostname: match[3],
      port: match[5],
      path: match[6],
      search: match[7],
      hash: match[8]
    };
  },
  mergeUrl: function (data) {
    let protocol = '';
    let port = '';
    if (data.protocol) {
      protocol = data.protocol + '://';
    }
    if (data.port) {
      port = ':' + data.port;
    }
    return protocol + data.hostname + port + (data.path || '') + (data.search || '') + (data.hash || '');
  },
  parseUrlParam: function (param) {
    let query = {};
    let paramList;
    let item;
    let key;
    let value;
    if (!param) {
      return query;
    }
    if (param[0] === '?') {
      param = param.slice(1);
    }
    paramList = param.split('&');
    for (let i = 0, len = paramList.length; i < len; i++) {
      if (paramList[i]) {
        item = paramList[i].split('=');
        key = decodeURIComponent(item[0]);
        if (key.match(/\[\]$/)) {
          key = key.slice(0, key.length - 2);
        }
        if (item.length > 1) {
          value = decodeURIComponent(item[1]);
        } else {
          value = '';
        }
        if (query.hasOwnProperty(key)) {
          query[key].push(value);
        } else {
          query[key] = [value];
        }
      }
    }
    return query;
  },
  mergeUrlParam: function (data) {
    let param = [];
    let suffix = '';
    let len;
    for (let key in data) {
      if (data.hasOwnProperty(key)) {
        len = data[key].length;
        if (len > 1) {
          suffix = '[]';
        } else {
          suffix = '';
        }
        if (Array.isArray(data[key])) {
          for (var i = 0; i < len; i++) {
            param.push(encodeURIComponent(key) + suffix + '=' + encodeURIComponent(data[key][i]));
          }
        } else {
          param.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
        }
      }
    }
    return param.join('&');
  },
  getUrlParam: function (name, url) {
    url = url || location.href;
    let urlObj = this.parseUrl(url);
    let urlQuery = this.parseUrlParam(urlObj.search);
    if (urlQuery.hasOwnProperty(name)) {
      return urlQuery[name][urlQuery[name].length - 1];
    }
    return undefined;
  },
  setUrlParam: function (name, value, url) {
    url = url || location.href;
    let urlObj = this.parseUrl(url);
    let urlQuery = this.parseUrlParam(urlObj.search);
    urlQuery[name] = [value];
    urlObj.search = '?' + this.mergeUrlParam(urlQuery);
    return this.mergeUrl(urlObj);
  },
  setUrlParams: function (params, url) {
    url = url || location.href;
    let urlObj = this.parseUrl(url);
    let urlQuery = this.parseUrlParam(urlObj.search);
    for (var key in params) {
      if (params.hasOwnProperty(key)) {
        urlQuery[key] = params[key];
      }
    }
    urlObj.search = '?' + this.mergeUrlParam(urlQuery);
    return this.mergeUrl(urlObj);
  }
};

export default Url;
